#include "MortgageCalculator.h"

double MortgageCalculator::getUserInput	(char* inputMessage) {
	char userInputData[50];		//raw user input
	double userInputValue = -1;	//user input as a double
	
	/*
	*	continue to prompt the user for input until they input a value >= 0
	*/
	while (userInputValue < 0) {
		cout << inputMessage;
		cin.getline(userInputData, 50, '\n');
		userInputValue = atof(userInputData);

		if (userInputValue < 0)		cout << "Invalid Value" << endl;
	} //while

	return userInputValue;
} //getUserInput (char* inputMessage)

bool MortgageCalculator::initLoan() {
	loan.setLoanAmount(getUserInput("Loan Amount: "));

	/*
	*	end the testing when the user inputs 0 for the loan amount
	*/
	if (loan.getLoanAmount() == 0) return false;

	loan.setNumberOfPayments((int)getUserInput("Number of Payments: "));
	loan.setAnnualInterestRate(getUserInput("Annual Interest Rate: "));

	return true;
} //initLoan()

void MortgageCalculator::display() {
	cout.setf(ios::fixed);
	cout.setf(ios::showpoint);
	cout.precision(2);

	cout << endl << "Loan Information" << endl << "-------------------------" << endl;
	cout << "Loan Amount: $" << loan.getLoanAmount() << endl;
	cout << "Number of Payments: " << loan.getNumberOfPayments() << endl;
	cout << "Annual Interest Rate: " << loan.getAnnualInterestRate() << "%" << endl;
	cout << "Monthly Payment: $" << loan.calculateMonthlyPayment() << endl;
	cout << "Loan Total Payment: $" << loan.calculateTotalAmountPaid() << endl;
	cout << "Loan Total Paid Interest: $" << loan.calculateTotalInterest() << endl;
	cout << endl;
} //toString()

void MortgageCalculator::execute() {
	/*
	*	Display loan information until init loan has failed
	*/
	while (initLoan()) display();

	getchar();
} //execute()